package cn.com.headfree.salary.service.impl;

import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.callback.call.FileServerCall;
import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.entity.TouXiangVo;
import cn.com.headfree.salary.service.WordExportService;
import cn.com.headfree.util.*;
import cn.com.headfree.vo.*;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.data.style.TableStyle;
import feign.Response;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 导出word工具类
 */
@Service("wordExportService")
public class WordExportServiceImpl implements WordExportService {

    private static final Logger log = LogManager.getLogger(WordExportServiceImpl.class);

    @Autowired
    private FileServerCall fileServerCall;

    /**
     * 在本地生成word文档
     * @param response
     * @param data              结果对象
     * @param filename            本地生成word名称
     */
    @Override
    public void exportWord(HttpServletResponse response, SalaryVo data, String filename , String erpNo,String userName){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年M月d日");
        FileOutputStream out=null;
        XWPFTemplate template = null;
        InputStream inputStream = null;
        File file = null;
        PaymentData datas = new PaymentData();//数据封装
        //样式
        Style textStyle = new Style();
        TableStyle headStyle = new TableStyle();
        TableStyle rowStyle = new TableStyle();
        rowStyle.setAlign(STJc.Enum.forInt(2));
        textStyle.setFontFamily("仿宋_GB2312");
        textStyle.setFontSize(12);
        textStyle.setColor("000000");
        headStyle.setAlign(STJc.CENTER);

        Calendar date = Calendar.getInstance();
        int currentYear = Integer.valueOf(date.get(Calendar.YEAR));
        int lastYear = currentYear - 1;
        String startDate = lastYear+"年1月1日";
        String endDate = lastYear+"年12月31日";
        datas.setYear(lastYear+"");
        datas.setStartTime(startDate);
        datas.setEndTime(endDate);

        //基本信息
        TEmpBaseInfo baseInfo = data.gettEmpBaseInfo();
        Date jobDate = null;//参加工作时间--工龄使用
        if(baseInfo != null){
            jobDate = baseInfo.getJobDate();
            datas.setName(baseInfo.getName());
            datas.setIdCardNo(baseInfo.getIdCardNo());
            datas.setNation(baseInfo.getNation());
            datas.setSex(baseInfo.getSex());
            datas.setBirthPlace(baseInfo.getBirthPlace());
            datas.setPolitiStatus(baseInfo.getPolitiStatus());
            if(baseInfo.getBirthDate() != null){
                datas.setBirthDate(sdf.format(baseInfo.getBirthDate()));
            }
            if(baseInfo.getJobDate() != null){
                datas.setJobDate(sdf.format(baseInfo.getJobDate()));
            }
            datas.setDept(baseInfo.getDept());//部门
            datas.setCurPost(baseInfo.getCurPost());//岗位
            datas.setMobilePhone(baseInfo.getMobilePhone());
        }
        //职务级别及时间
        WorkExpInfoVo workExpInfoVo = data.getWorkExpInfoVo();
        if(workExpInfoVo != null){
            String postInfo = "";
            if(workExpInfoVo.getRank() != null){
                postInfo = postInfo + workExpInfoVo.getRank();
            }
            if(workExpInfoVo.getStartTime() != null){
                postInfo = postInfo + " " + sdf.format(workExpInfoVo.getStartTime());
            }
            datas.setPostInfo(postInfo);
        }
        //全日制学历信息
        EducateInfoVo fullTimeEdu = data.getFullTimeEduInfo();
        if(fullTimeEdu != null){
            String eduAndDegree = "";
            if(fullTimeEdu.getEducName() != null){
                eduAndDegree = fullTimeEdu.getEducName();
            }
            if(fullTimeEdu.getDegreeName() != null){
                eduAndDegree = eduAndDegree + " " + fullTimeEdu.getDegreeName();
            }
            datas.setEmployEduAndDegree(eduAndDegree);
            String employInfo = "";
            if(fullTimeEdu.getAcquTime() != null){
                employInfo = sdf.format(fullTimeEdu.getAcquTime());
            }
            if(fullTimeEdu.getGradSchool() != null){
                employInfo = employInfo + " " + fullTimeEdu.getGradSchool();
            }
            employInfo = employInfo + fullTimeEdu.getMajor();
            datas.setEmployEduInfo(employInfo);
        }
        //最高学历信息
        EducateInfoVo highEduInfo = data.getHighEduInfo();
        if(highEduInfo != null){
            String eduAndDegree = "";
            if(highEduInfo.getEducName() != null){
                eduAndDegree = highEduInfo.getEducName();
            }
            if(highEduInfo.getDegreeName() != null){
                eduAndDegree = eduAndDegree + " " + highEduInfo.getDegreeName();
            }
            datas.setHighEduAndDegree(eduAndDegree);
            String highInfo = "";
            if(highEduInfo.getAcquTime() != null){
                highInfo = sdf.format(highEduInfo.getAcquTime());
            }
            if(highEduInfo.getGradSchool() != null){
                highInfo = highInfo + " " + highEduInfo.getGradSchool();
            }
            if(highEduInfo.getMajor() != null){
                highInfo = highInfo + highEduInfo.getMajor();
            }
            datas.setHighEduInfo(highInfo);
        }
        //专业技术资格及时间
        ProTecInfoVo proTecInfoVo = data.getProTecInfoVo();
        if(proTecInfoVo != null){
            String proTecInfo = "";
            if(proTecInfoVo.getSubjName() != null){
                proTecInfo = proTecInfoVo.getSubjName();
            }
            if(proTecInfoVo.getAcquTime() != null){
                proTecInfo = proTecInfo + " " + sdf.format(proTecInfoVo.getAcquTime());
            }
            datas.setProTecInfo(proTecInfo);
        }

        //技能等级及时间
        SkillLevelInfoVo skillLevelInfoVo = data.getSkillLevelInfoVo();
        if(skillLevelInfoVo != null){
            String skillInfo = "";
            if(skillLevelInfoVo.getTechType() != null){
                skillInfo = skillLevelInfoVo.getTechType();
            }
            if(skillLevelInfoVo.getAcquTime() != null){
                skillInfo = skillInfo + " " + sdf.format(skillLevelInfoVo.getAcquTime());
            }
            datas.setSkillInfo(skillInfo);
        }

        //人才称号
        ExpertTitleVo expertTitleVo = data.getExpertTitleVo();
        if(expertTitleVo != null){
            String expertNameAndLevel = "";
            if(expertTitleVo.getLevelName() != null){
                expertNameAndLevel = expertTitleVo.getLevelName();
            }
            if(expertTitleVo.getEmpTitle() != null){
                expertNameAndLevel = expertNameAndLevel + " " + expertTitleVo.getEmpTitle();
            }
            String expertTime = "";
            if(expertTitleVo.getAcquTime() != null){
                expertTime = sdf.format(expertTitleVo.getAcquTime());
            }
            datas.setExpertNameAndLevel(expertNameAndLevel);
            datas.setExpertTime(expertTime);
        }

        //上年度绩效
        YearEffectVo yearEffectVo = data.getYearEffectVo();
        if(yearEffectVo != null){
            datas.setYearEffect(yearEffectVo.getPerforGrade());
        }

        //学历列表
        EducateInfoTable educateInfoTable = new EducateInfoTable();
        List<EducateInfoVo> educateInfoVos = data.getEducateInfoVos();
        List<RowRenderData> eduRowList = new ArrayList<>();
        if(educateInfoVos != null && educateInfoVos.size() > 0){
            for(int i=educateInfoVos.size()-1;i>=0;i--){
                if(educateInfoVos.get(i).getIsHide() == null || !(educateInfoVos.get(i).getIsHide().equals("0"))) {
                    String acquTime = "";
                    if (educateInfoVos.get(i).getAcquTime() != null) {
                        acquTime = sdf.format(educateInfoVos.get(i).getAcquTime());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(acquTime, textStyle),
                            new TextRenderData(educateInfoVos.get(i).getGradSchool(), textStyle),
                            new TextRenderData(educateInfoVos.get(i).getSchoolType(), textStyle),
                            new TextRenderData(educateInfoVos.get(i).getMajor(), textStyle),
                            new TextRenderData(educateInfoVos.get(i).getEducName(), textStyle),
                            new TextRenderData(educateInfoVos.get(i).getDegreeName(), textStyle));
                    row.setRowStyle(rowStyle);
                    eduRowList.add(row);
                }
            }
        }
        educateInfoTable.setEducateInfos(eduRowList);
        datas.setEducateInfoTable(educateInfoTable);

        //专业技术资格列表
        ProTecInfoTable proTecInfoTable = new ProTecInfoTable();
        List<ProTecInfoVo> tecInfoVos = data.getProTecInfoVos();
        List<RowRenderData> proRowList = new ArrayList<>();
        if(tecInfoVos != null && tecInfoVos.size() > 0){
            for(int i=tecInfoVos.size()-1;i>=0;i--){
                if(tecInfoVos.get(i).getIsHide() == null || !(tecInfoVos.get(i).getIsHide().equals("0"))) {
                    String acquTime = "";
                    if (tecInfoVos.get(i).getAcquTime() != null) {
                        acquTime = sdf.format(tecInfoVos.get(i).getAcquTime());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(acquTime, textStyle),
                            new TextRenderData(tecInfoVos.get(i).getSubjName(), textStyle),
                            new TextRenderData(tecInfoVos.get(i).getSubjSet(), textStyle),
                            new TextRenderData(tecInfoVos.get(i).getSubjLevel(), textStyle),
                            new TextRenderData(tecInfoVos.get(i).getCertNo(), textStyle));
                    row.setRowStyle(rowStyle);
                    proRowList.add(row);
                }
            }
        }
        proTecInfoTable.setProTecInfos(proRowList);
        datas.setProTecInfoTable(proTecInfoTable);

        //技能等级列表
        SkillLevelInfoTable skillLevelInfoTable = new SkillLevelInfoTable();
        List<SkillLevelInfoVo> skillLevelInfoVoList = data.getSkillLevelInfoVos();
        List<RowRenderData> skillRowList = new ArrayList<>();
        if(skillLevelInfoVoList != null && skillLevelInfoVoList.size() > 0){
            for(int i=skillLevelInfoVoList.size()-1;i>=0;i--){
                if(skillLevelInfoVoList.get(i).getIsHide() == null || !(skillLevelInfoVoList.get(i).getIsHide().equals("0"))) {
                    String acquTime = "";
                    if (skillLevelInfoVoList.get(i).getAcquTime() != null) {
                        acquTime = sdf.format(skillLevelInfoVoList.get(i).getAcquTime());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(acquTime, textStyle),
                            new TextRenderData(skillLevelInfoVoList.get(i).getTechType(), textStyle),
                            new TextRenderData(skillLevelInfoVoList.get(i).getQualifiLevel(), textStyle),
                            new TextRenderData(skillLevelInfoVoList.get(i).getCertNo(), textStyle));
                    row.setRowStyle(rowStyle);
                    skillRowList.add(row);
                }
            }
        }
        skillLevelInfoTable.setSkillLevelInfos(skillRowList);
        datas.setSkillLevelInfoTable(skillLevelInfoTable);

        //专业竞赛考试列表
        ContestInfoTable contestInfoTable = new ContestInfoTable();
        List<ContestInfoVo> contestInfoVos = data.getContestInfoVos();
        List<RowRenderData> contestRowList = new ArrayList<>();
        if(contestInfoVos != null && contestInfoVos.size() > 0){
            for(int i=contestInfoVos.size()-1;i>=0;i--){
                if(contestInfoVos.get(i).getIsHide() == null || !(contestInfoVos.get(i).getIsHide().equals("0"))) {
                    String awardDate = "";
                    if (contestInfoVos.get(i).getAwardDate() != null) {
                        awardDate = sdf.format(contestInfoVos.get(i).getAwardDate());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(awardDate, textStyle),
                            new TextRenderData(contestInfoVos.get(i).getCompName(), textStyle),
                            new TextRenderData(contestInfoVos.get(i).getHostOrg(), textStyle),
                            new TextRenderData(contestInfoVos.get(i).getAwardLevel(), textStyle),
                            new TextRenderData(contestInfoVos.get(i).getCompRank(), textStyle));
                    row.setRowStyle(rowStyle);
                    contestRowList.add(row);
                }
            }
        }
        contestInfoTable.setContestInfos(contestRowList);
        datas.setContestInfoTable(contestInfoTable);

        //人才称号列表
        ExpertTitleInfoTable expertTitleInfoTable = new ExpertTitleInfoTable();
        List<ExpertTitleVo> expertTitleVos = data.getExpertTitleVos();
        List<RowRenderData> expertRowList = new ArrayList<>();
        if(expertTitleVos != null && expertTitleVos.size() > 0){
            for(int i=expertTitleVos.size()-1;i>=0;i--){
                if(expertTitleVos.get(i).getIsHide() == null || !(expertTitleVos.get(i).getIsHide().equals("0"))) {
                    String acquTime = "";
                    if (expertTitleVos.get(i).getAcquTime() != null) {
                        acquTime = sdf.format(expertTitleVos.get(i).getAcquTime());
                    }
                    String startTime = "";
                    String endTime = "";
                    if (expertTitleVos.get(i).getEmpTermStart() != null) {
                        startTime = sdf1.format(expertTitleVos.get(i).getEmpTermStart());
                    }
                    if (expertTitleVos.get(i).getEmpTermStart() != null) {
                        endTime = sdf1.format(expertTitleVos.get(i).getEmpTermEnd());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(acquTime, textStyle),
                            new TextRenderData(expertTitleVos.get(i).getMajor(), textStyle),
                            new TextRenderData(expertTitleVos.get(i).getLevelName(), textStyle),
                            new TextRenderData(expertTitleVos.get(i).getEmpTitle(), textStyle),
                            new TextRenderData(startTime+"-"+endTime, textStyle));
                    row.setRowStyle(rowStyle);
                    expertRowList.add(row);
                }
            }
        }
        expertTitleInfoTable.setExpertTitleInfos(expertRowList);
        datas.setExpertTitleInfoTable(expertTitleInfoTable);

        //科技管理创新列表
        TecManTable tecManTable = new TecManTable();
        List<TecManInnovVo> tecManInnovVos = data.getTecManInnovVos();
        List<RowRenderData> tecManRowList = new ArrayList<>();
        if(tecManInnovVos != null && tecManInnovVos.size() > 0){
            for(int i=tecManInnovVos.size()-1;i>=0;i--){
                if(tecManInnovVos.get(i).getIsHide() == null || !(tecManInnovVos.get(i).getIsHide().equals("0"))) {
                    String awardDate = "";
                    if (tecManInnovVos.get(i).getAwardDate() != null) {
                        awardDate = sdf.format(tecManInnovVos.get(i).getAwardDate());
                    }
                    String sort = "";
                    if(tecManInnovVos.get(i).getRank() != null && tecManInnovVos.get(i).getRank() != 0){
                        sort = tecManInnovVos.get(i).getRank()+"";
                        if(tecManInnovVos.get(i).getTotal() != 0){
                            sort = sort + "/" + tecManInnovVos.get(i).getTotal();
                        }
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(awardDate, textStyle),
                            new TextRenderData(tecManInnovVos.get(i).getAchiName(), textStyle),
                            new TextRenderData(tecManInnovVos.get(i).getAwardName(), textStyle),
                            new TextRenderData(tecManInnovVos.get(i).getAwardLevel(), textStyle),
                            new TextRenderData(tecManInnovVos.get(i).getAwardGrade(), textStyle),
                            new TextRenderData(tecManInnovVos.get(i).getAwardOrg(), textStyle),
                            new TextRenderData(sort, textStyle));
                    row.setRowStyle(rowStyle);
                    tecManRowList.add(row);
                }
            }
        }
        tecManTable.setTecManInfos(tecManRowList);
        datas.setTecManTable(tecManTable);

        //技艺革新列表
        TecInnovInfoTable tecInnovInfoTable = new TecInnovInfoTable();
        List<TecInnovInfoVo> tecInnovInfoVos = data.getTecInnovInfoVos();
        List<RowRenderData> tecInnovRowList = new ArrayList<>();
        if(tecInnovInfoVos != null && tecInnovInfoVos.size() > 0){
            for(int i=tecInnovInfoVos.size()-1;i>=0;i--){
                if(tecInnovInfoVos.get(i).getIsHide() == null || !(tecInnovInfoVos.get(i).getIsHide().equals("0"))) {
                    String awardDate = "";
                    if (tecInnovInfoVos.get(i).getAwardDate() != null) {
                        awardDate = sdf.format(tecInnovInfoVos.get(i).getAwardDate());
                    }
                    String sort = "";
                    if(tecInnovInfoVos.get(i).getRank() != null && tecInnovInfoVos.get(i).getRank() != 0){
                        sort = tecInnovInfoVos.get(i).getRank()+"";
                        if(tecInnovInfoVos.get(i).getTotal() != 0){
                            sort = sort + "/" + tecInnovInfoVos.get(i).getTotal();
                        }
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(awardDate, textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getAchiName(), textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getAwardName(), textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getAwardLevel(), textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getAwardGrade(), textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getAwardOrg(), textStyle),
                            new TextRenderData(tecInnovInfoVos.get(i).getGroupName(), textStyle),
                            new TextRenderData(sort, textStyle));
                    row.setRowStyle(rowStyle);
                    tecInnovRowList.add(row);
                }
            }
        }
        tecInnovInfoTable.setTecInnovInfos(tecInnovRowList);
        datas.setTecInnovInfoTable(tecInnovInfoTable);

        //工龄
        EmployLengthTable employLengthTable = new EmployLengthTable();
        List<RowRenderData> employRowList = new ArrayList<>();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
        int employLength = 0;
        String jobTime = null;
        if(jobDate != null){
            int jobYear = Integer.valueOf(sdf2.format(jobDate));
            employLength = currentYear - jobYear + 1;
            jobTime = sdf.format(jobDate);
        }
        RowRenderData row0 = RowRenderData.build(
                new TextRenderData(jobTime, textStyle),
                new TextRenderData(employLength+"", textStyle));
        row0.setRowStyle(rowStyle);
        employRowList.add(row0);
        employLengthTable.setEmployLengthInfo(employRowList);
        datas.setEmployLengthTable(employLengthTable);

        //专利列表
        PatentInfoTable patentInfoTable = new PatentInfoTable();
        List<PatentInfoVo> patentInfoVos = data.getPatentInfoVos();
        List<RowRenderData> patentRowList = new ArrayList<>();
        if(patentInfoVos != null && patentInfoVos.size() > 0){
            for(int i=patentInfoVos.size()-1;i>=0;i--){
                if(patentInfoVos.get(i).getIsHide() == null || !(patentInfoVos.get(i).getIsHide().equals("0"))) {
                    String applyDate = "";
                    String declDate = "";
                    if (patentInfoVos.get(i).getApplyDate() != null) {
                        applyDate = sdf.format(patentInfoVos.get(i).getApplyDate());
                    }
                    if (patentInfoVos.get(i).getDeclDate() != null) {
                        declDate = sdf.format(patentInfoVos.get(i).getDeclDate());
                    }
                    String sort = "";
                    if(patentInfoVos.get(i).getRank() != null && patentInfoVos.get(i).getRank() != 0){
                        sort = patentInfoVos.get(i).getRank()+"";
                        if(patentInfoVos.get(i).getTotal() != 0){
                            sort = sort + "/" + patentInfoVos.get(i).getTotal();
                        }
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(patentInfoVos.get(i).getPatentName(), textStyle),
                            new TextRenderData(patentInfoVos.get(i).getPatentType(), textStyle),
                            new TextRenderData(patentInfoVos.get(i).getPatentNo(), textStyle),
                            new TextRenderData(applyDate, textStyle),
                            new TextRenderData(userName, textStyle),
                            new TextRenderData(declDate, textStyle),
                            new TextRenderData(sort, textStyle));
                    row.setRowStyle(rowStyle);
                    patentRowList.add(row);
                }
            }
        }
        patentInfoTable.setPatentInfos(patentRowList);
        datas.setPatentInfoTable(patentInfoTable);

        //论文著作列表
        AcadeAchiveTable acadeAchiveTable = new AcadeAchiveTable();
        List<AcadeAchiveVo> acadeAchiveVos = data.getAcadeAchiveVos();
        List<RowRenderData> acadeRowList = new ArrayList<>();
        if(acadeAchiveVos != null && acadeAchiveVos.size() > 0){
            for(int i=acadeAchiveVos.size()-1;i>=0;i--){
                if(acadeAchiveVos.get(i).getIsHide() == null || !(acadeAchiveVos.get(i).getIsHide().equals("0"))) {
                    String putTime = "";
                    if (acadeAchiveVos.get(i).getPubTime() != null) {
                        putTime = sdf.format(acadeAchiveVos.get(i).getPubTime());
                    }
                    String sort = "";
                    if(acadeAchiveVos.get(i).getRank() != null){
                        sort = acadeAchiveVos.get(i).getRank()+"";
                        if(acadeAchiveVos.get(i).getTotal() != 0){
                            sort = sort + "/" + acadeAchiveVos.get(i).getTotal();
                        }
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(putTime, textStyle),
                            new TextRenderData(acadeAchiveVos.get(i).getPubType(), textStyle),
                            new TextRenderData(acadeAchiveVos.get(i).getAcadName(), textStyle),
                            new TextRenderData(acadeAchiveVos.get(i).getPubName(), textStyle),
                            new TextRenderData(acadeAchiveVos.get(i).getIssn(), textStyle),
                            new TextRenderData("", textStyle),
                            new TextRenderData(acadeAchiveVos.get(i).getPublisher(), textStyle),
                            new TextRenderData(sort, textStyle));
                    row.setRowStyle(rowStyle);
                    acadeRowList.add(row);
                }
            }
        }
        acadeAchiveTable.setAcadeAchiveInfos(acadeRowList);
        datas.setAcadeAchiveTable(acadeAchiveTable);

        //个人荣誉列表
        HonorInfoTable honorInfoTable = new HonorInfoTable();
        List<HonorDatasVo> honorDatasVos = data.getHonorDatasVos();
        List<RowRenderData> honorRowList = new ArrayList<>();
        if(honorDatasVos != null && honorDatasVos.size() > 0){
            for(int i=honorDatasVos.size()-1;i>=0;i--){
                if(honorDatasVos.get(i).getIsHide() == null || !(honorDatasVos.get(i).getIsHide().equals("0"))) {
                    String acquTime = "";
                    if (honorDatasVos.get(i).getAcquTime() != null) {
                        acquTime = sdf.format(honorDatasVos.get(i).getAcquTime());
                    }
                    RowRenderData row = RowRenderData.build(
                            new TextRenderData(acquTime, textStyle),
                            new TextRenderData(honorDatasVos.get(i).getHonorName(), textStyle),
                            new TextRenderData(honorDatasVos.get(i).getHonorLevel(), textStyle),
                            new TextRenderData(honorDatasVos.get(i).getHonorType(), textStyle),
                            new TextRenderData(honorDatasVos.get(i).getAwardUnit(), textStyle));
                    row.setRowStyle(rowStyle);
                    honorRowList.add(row);
                }
            }
        }
        honorInfoTable.setHonorInfos(honorRowList);
        datas.setHonorInfoTable(honorInfoTable);

        try {
//            //获取头像
//            TouXiangVo iamges = getTouXiang(erpNo);
//            if(iamges != null && iamges.getInputStream()!=null){
//                datas.setPic(new PictureRenderData(100, 160,".jpg",
//                        iamges.getInputStream()));
//            }
            ClassPathResource resource = new ClassPathResource("/templates/salary_info.docx");
            inputStream = resource.getInputStream();
            Configure config = Configure.newBuilder().bind("tec_man_table", new TecManTablePolicy()).bind("tec_innov_table", new TecInnovInfoTablePolicy())
                    .bind("employ_length_table", new EmployLengthTablePolicy()).bind("patent_info_table", new PatentInfoTablePolicy())
                    .bind("educate_info_table", new EducateInfoTablePolicy()).bind("pro_tec_info_table", new ProTecInfoTablePolicy())
                    .bind("skill_level_info_table", new SkillLevelInfoTablePolicy()).bind("con_test_info_table", new ConTestInfoTablePolicy())
                    .bind("expert_title_info_table", new ExpertInfoTablePolicy()).bind("acade_achive_table", new AcadeAchiveTablePolicy())
                    .bind("honor_info_table", new HonorInfoTablePolicy()).build();
            template = XWPFTemplate.compile(inputStream,config).render(datas);
            String tempfilePath= UUID.randomUUID() +".docx";
            out = new FileOutputStream(tempfilePath);
            template.write(out);
            out.flush();
            responseWord(response,tempfilePath,filename);
            //删除临时文件
            file = new File(tempfilePath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (out!=null){
                    out.close();
                }
                if (template!=null){
                    template.close();
                }
                if (file != null) {// 删除临时文件
                    if(file.exists()){
                        file.delete();
                    }
                }
            }catch (Exception e){
                log.error("关闭异常",e);
            }
        }
    }

    static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");

    private void responseWord(HttpServletResponse response,String path,String filename){
        File file = null;
        InputStream in = null;
        ServletOutputStream out = null;
        try{
            String downloadFileName=filename+simpleDateFormat.format(new Date())+".docx";
            file = new File(path);
            in = new FileInputStream(file);
            response.setCharacterEncoding("utf-8");
            response.setHeader("Cache-Control", "");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(downloadFileName, "UTF-8"))));
            out = response.getOutputStream();
            byte[] buffer = new byte[512];
            int bytesToRead = -1;
            // 用响应对象response中的输出流读取生成好的文件
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
        }catch (Exception ex){
            log.error("product word fail..",ex);
        }finally {
            if (in != null) {
                try {
                    in.close();
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    log.error("删除删除临时文件出错:" + e);
                }
            }
        }
    }

    /**
     * 获取头像
     * @param erpNo
     * @return
     */
    public TouXiangVo getTouXiang(String erpNo){
        TouXiangVo touXiangVo = null;
        InputStream inputStream = null;
        String fileId=null;
        String name=null;
        //通过ERP号查询头像Id
        ResultData resultData = fileServerCall.findFileList("personManage",erpNo,"userHead");
        JSONArray jsonArray =null;
        JSONObject obj =null;
        if(null != resultData) {
            log.info("获取头像数据为--"+resultData.toString());
            if(null != resultData.getData()) {
                jsonArray = JSONArray.fromObject(resultData.getData());
                if((null != jsonArray)&&(0 < jsonArray.size())) {
                    for(int i=0;i<jsonArray.size();i++) {
                        obj = (JSONObject) jsonArray.get(i);
                        if(null != obj) {
                            fileId = obj.getString("id");
                            name = obj.getString("oldFileName");
                        }
                    }
                }
            }
        }
        //下载文件
        if(fileId != null){
            try {
                Response response = fileServerCall.downloadFile(fileId);

                //判断是否下载成功
                if(response.status()==200) {
                    Response.Body body = response.body();
                    inputStream = body.asInputStream();
//                    inputStream = new FileInputStream(new File("test_one.png"));
                    touXiangVo = new TouXiangVo();
                    touXiangVo.setName(name);
                    touXiangVo.setInputStream(inputStream);
                }

            }catch (Exception e){
                log.error("下载文件失败！", e);
            }

        }

        return touXiangVo;
    }


}
